/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ai.study.ollama.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.ollama.api.OllamaOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @author yuluo
 * @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
 */

@RestController
@RequestMapping("/ollama/client")
@AllArgsConstructor
@Tag(name = "chatClient测试")
public class OllamaChatClientController {

	private static final String DEFAULT_PROMPT = "你好，介绍下你自己！请用中文回答。";

	@Autowired
	@Qualifier("ollamaChatClient")
	private final ChatClient ollamaChatClient;

	// public OllamaChatClientController(OllamaChatModel ollamaChatModel) {
	//
	// 	// 构造时，可以设置 ChatClient 的参数
	// 	// {@link org.springframework.ai.chat.client.ChatClient};
	// 	this.ollamaChatClient = ChatClient.builder(ollamaChatModel)
	// 			// 实现 Logger 的 Advisor
	// 			.defaultAdvisors(
	// 					new SimpleLoggerAdvisor()
	// 			)
	// 			// 设置 ChatClient 中 ChatModel 的 Options 参数
	// 			.defaultOptions(
	// 					OllamaOptions.builder()
	// 							.topP(0.7)
	// 							.model("llama3")
	// 							.build()
	// 			)
	// 			.build();
	// }

	/**
	 * ChatClient 简单调用
	 */
	@GetMapping("/simple/chat")
	@Operation(summary = "chatClient call 简单调用")
	public String simpleChat(@RequestParam(value = "prompt", defaultValue = DEFAULT_PROMPT) String prompt) {

		return ollamaChatClient.prompt(prompt).call().content();
	}

	/**
	 * ChatClient 流式调用
	 */
	@GetMapping("/stream/chat")
	@Operation(summary = "chatClient stream 简单调用")
	public Flux<String> streamChat(@RequestParam(value = "prompt", defaultValue = DEFAULT_PROMPT) String prompt,
								   HttpServletResponse response) {

		response.setCharacterEncoding("UTF-8");
		return ollamaChatClient.prompt(prompt).stream().content();
	}

}
